package com.wanxi.servlet.cuisine;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.model.CuisineModel;
import com.wanxi.model.ResultModel;
import com.wanxi.service.CuisineService;
import com.wanxi.service.impl.CuisineServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/cuisine/add")
public class CuisineAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.获取前端的值
        String name=req.getParameter("name");
//        3.封装到model
        CuisineModel cuisineModel=new CuisineModel();
        cuisineModel.setName(Tool.nullToStr(name));
//        4.调用service
        CuisineService cuisineService=new CuisineServiceImpl();
//        5.得到返回值
        ResultModel resultModel=cuisineService.add(cuisineModel);
//        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));
    }
}
